package st.business.tepth;

import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.FrameLayout;
import android.widget.Toast;

import st.business.tepth.base.BaseActivity;
import st.business.tepth.business.RootDelegate;
import st.business.tepth.ic.IcManager;
import st.business.tepth.ic.LotusCard;


/**
 * @author zhangyu
 * @date 2019/1/14
 */
public class BusTunActivity extends BaseActivity {
    private static final int m_nVID = 1306;
    private static final int m_nPID = 20763;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initContainer(savedInstanceState);
        //tryGetUsbPermission();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //sunregisterReceiver(mUsbPermissionActionReceiver);
        //IcManager.getInstance().closeDevice();
    }

    private void initContainer(@Nullable Bundle savedInstanceState) {
        final FrameLayout container = new FrameLayout(this);
        container.setId(R.id.delegate_container);
        setContentView(container);
        RootDelegate rootDelegate = findFragment(RootDelegate.class);
        if (rootDelegate == null) {
            loadRootFragment(R.id.delegate_container,
                    RootDelegate.newInstance());
        }
    }

    private final BroadcastReceiver mUsbPermissionActionReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (LotusCard.ACTION_USB_PERMISSION.equals(action)) {
                synchronized (this) {
                    UsbDevice usbDevice = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
                    if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
                        if (null != usbDevice) {
                            IcManager.getInstance().openDevice();
                        }
                    } else {
                        Toast.makeText(context, String.valueOf("Permission denied for device" + usbDevice), Toast.LENGTH_LONG).show();
                    }
                }
            }
        }
    };
    UsbManager mUsbManager;
    private static final String ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION";

    private void tryGetUsbPermission() {
        mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
        IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
        registerReceiver(mUsbPermissionActionReceiver, filter);
        PendingIntent mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
        for (final UsbDevice usbDevice : mUsbManager.getDeviceList().values()) {
            if ((m_nVID == usbDevice.getVendorId())
                    && (m_nPID == usbDevice.getProductId())) {
                if (mUsbManager.hasPermission(usbDevice)) {
                    IcManager.getInstance().openDevice();
                } else {
                    mUsbManager.requestPermission(usbDevice, mPermissionIntent);
                }
            }
        }
    }
}
